//
//  RequestUtil.m
//  SywApp
//
//  Created by chewyong on 2018/1/16.
//
//

#import "RequestUtil.h"

@implementation RequestUtil

/**
 * 从响应字典中获取消息字符串
 * @param responseDic 包含响应数据的字典
 * @return 返回从响应字典中解析出的消息字符串。如果消息不为空，则返回消息；否则根据代码返回默认的错误消息。
 */
+ (NSString *)responseMessage:(NSDictionary *)responseDic {
    NSString *returnMsg = @"";
    returnMsg = [responseDic objectForKey:@"message"];
    if (returnMsg && ![returnMsg isKindOfClass:[NSNull class]] && returnMsg.length > 0) {
        return returnMsg;
    }

    NSString *code = [responseDic objectForKey:@"code"];
    if (code && ![code isKindOfClass:[NSNull class]]) {
        //根据code值处理返回的消息
        //示例代码已被注释，可以根据实际情况取消注释并修改
        //if ([code isEqualToString:@"0001"]) {
        //    returnMsg = @"错误消息1";
        //} else if ([code isEqualToString:@"0002"]) {
        //    returnMsg = @"错误消息2";
        //} ...
        returnMsg = @"请求失败";
    } else {
        returnMsg = @"请求失败";
    }

    return returnMsg;
}

@end
